import { Destroyable, react, reactJson, Event, track, HasOwner, reactJsonWithUndefined } from "xbsj-xe2/dist-node/xe2-base-utils";
import { MyProjectManager } from "../MyProjectManager";
import { Child, TransformedChild } from '@/scripts/dataTypes/interface'
import { ObjResettingWithEvent, SceneObject, Viewer } from 'xbsj-xe2/dist-node/xe2-base-objects';
import { ESRtsFeatureEditing } from "../esobjs/ESRtsFeatureEditing";
import { getsceneObjNumfromSceneTree } from "../utils";
import { ElMessage } from "element-plus";
import { ESRtsEexturedPolygon } from "../esobjs/ESRtsEexturedPolygon";
import { ESRtsPolygon } from "../esobjs/ESRtsPolygon";
import { ESFeTreeEditing } from "../esobjs/ESFeTreeEditing";

function isNumber(value: any): boolean {
    return typeof value === 'number' && !isNaN(value);
}
class PickResettting extends Destroyable {
    constructor(editTree: EditTree, owner: MyProjectManager) {
        super();
        const eSForest = owner.eSForestManager.eSForest
        const viewer = owner.activeViewer

        if (!viewer) return;
        this.dispose(viewer.clickEvent.don(async e => {

            if (!e.screenPosition) return;
            // 拾取
            const pick = await eSForest.pickTree(viewer, e.screenPosition)
            const obj = pick?.childPickedInfo?.childPickedInfo?.czmPickResult
            const instances = pick?.childPickedInfo?.sceneObject?.instances

            console.log("pick", pick);
            if (!obj || !obj.hasOwnProperty("_featureId") || this.isDestroyed()) return;

            // 隐藏旧的
            obj.show = false

            let sceneObject: any

            const trees = owner.eSForestManager.eSForest.trees
            const index = obj.featureId
            const { id, treeType, position, guanfu, xiongjing, zhixiagao, shugao } = trees[index]

            sceneObject = owner.createSceneObject(ESFeTreeEditing, id) as ESFeTreeEditing
            sceneObject.dispose(() => obj.show = true)

            sceneObject.position = position
            sceneObject.plotId = owner.eSForestManager.esForestPlotId
            sceneObject.time = owner.eSForestManager.esForestTime

            sceneObject.treeModelClassName = treeType
            sceneObject.shugao = shugao
            sceneObject.xiongjing = xiongjing
            sceneObject.zhixiagao = zhixiagao
            // @ts-ignore
            sceneObject.dongxiguanfu = guanfu[0]
            // @ts-ignore
            sceneObject.nanbeiguanfu = guanfu[1]
            const param = eSForest.formatData([trees[index]], eSForest.getTreeTypes())
            sceneObject.scale = param[0].scale
            sceneObject.url = obj._model._resource.url
            const rotation = pick.childPickedInfo.sceneObject.instances.find((instance:any)=>instance.id == index).rotation
            sceneObject.rotation = [rotation[0], rotation[1], rotation[2]];

            sceneObject.editing = true

            const treeItem = owner.createdTreeItem(owner.forestSceneTreeWrapper)
            treeItem.sceneObject = sceneObject;
            const name = sceneObject.treeModelClassName
            const num = getsceneObjNumfromSceneTree(sceneObject.typeName, owner.forestSceneTreeWrapper);
            treeItem.name = `编辑_${name}(${num})`
            owner.forestSceneTreeWrapper?.uiTree.clearAllSelectedItems()
            treeItem.uiTreeObject.selected = true

            editTree.treeInfo = [
                {
                    key: "ID",
                    value: sceneObject.id,
                },
                {
                    key: "树种",
                    value: sceneObject.treeModelClassName,
                },
                {
                    key: "树高",
                    value: sceneObject.shugao,
                },
                {
                    key: "胸径",
                    value: sceneObject.xiongjing,
                },
                {
                    key: "枝下高",
                    value: sceneObject.zhixiagao,
                },
                {
                    key: "东西冠幅",
                    value: sceneObject.dongxiguanfu,
                },
                {
                    key: "南北冠幅",
                    value: sceneObject.nanbeiguanfu,
                },
            ]

            owner.forestPropSceneTree = {
                createOrEdit: 'edit',
                propType: "addTree",
                sceneObject: sceneObject,
            }



        }))
    }
}

export class EditTree extends HasOwner<MyProjectManager> {

    // 开启拾取
    private _enablePick = this.dv(react<boolean>(false));
    get enablePick() { return this._enablePick.value; }
    get enablePickChanged() { return this._enablePick.changed; }
    set enablePick(value: boolean) { this._enablePick.value = value; }

    // 树木信息
    private _treeInfo = this.dv(reactJson<any>([]));
    get treeInfo() { return this._treeInfo.value; }
    get treeInfoChanged() { return this._treeInfo.changed; }
    set treeInfo(value: any) { this._treeInfo.value = value; }

    // 拾取处理
    private _pickResettting = this.dv(new ObjResettingWithEvent(this.enablePickChanged, () => {
        if (!this.enablePick) return undefined;
        return new PickResettting(this, this.owner);
    }))
    get pickResettting() { return this._pickResettting; }

    init() {
        this.enablePick = true
    }
    clear() {
        this.enablePick = false
        this.treeInfo = []
    }

    constructor(owner: MyProjectManager) {
        super(owner);
    }
}







